package mybatis.service;

import mybatis.mapper.UserMapper;
import mybatis.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class UserService {

    private static final Logger log = LoggerFactory.getLogger(UserService.class);

    @Transactional
    public void insert(ApplicationContext context) {
        UserMapper mapper = context.getBean("userMapper",UserMapper.class);
        int num = mapper.insert("333");
        //故意报异常测试事务
        //int a = 1/0;
        log.info("新增数量：{}", num);
    }


    public void selectList(ApplicationContext context) {
        UserMapper mapper = context.getBean("userMapper",UserMapper.class);
        List<User> users = mapper.selectList();
        for (User user : users) {
            log.info("查询结果： {}", user);
        }
    }

}
